package suanfa;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class largestValues {
  public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> ret=new ArrayList<>();
        if(root==null) return ret;
        Queue<TreeNode> q=new LinkedList<>();
        q.add(root);
        int size=0;
        while(!q.isEmpty()){
            size=q.size();
            int tmp=Integer.MIN_VALUE;
            for(int i=0;i<size;i++){
                TreeNode t=q.poll();
                tmp=Math.max(tmp,t.val);
                if(t.left!=null) q.add(t.left);
                if(t.right!=null) q.add(t.right);
            }
            ret.add(tmp);
        }
        return ret;
    }
}
